#pragma once
#include <iostream>
#include <string>
#include "dict.hpp"

using std::cout;
using std::endl;
using std::string;
#define BUFFERSIZE 1024

enum
{
    Success = 0,
    DivZero,
    ModZero,
    UnkownOper
};

class Task
{
public:
    Task(int rwfd, std::string clientip, uint16_t clientport)
        : rwfd_(rwfd), clientip_(clientip), clientport_(clientport_)
    {
    }

    void operator()()
    {
        run();
    }

private:
    void run()
    {
        dict d;
        d.init();
        char buffer[BUFFERSIZE];
        int real_nums = read(rwfd_, buffer, BUFFERSIZE);
        if (real_nums == -1)
        {
            lg(FATAL, "read fatal %d:%s", errno, strerror(errno)); // 这里
            exit(-1);
        }
        buffer[real_nums] = '\0';
        lg(INFO, "client info : %s", buffer); // 这里
        string handled_str;
        string tranret = d.tarnslate(buffer);
        handled_str += tranret;
        int ret = write(rwfd_, handled_str.c_str(), handled_str.size());
        if (ret == -1)
        { 
            lg(FATAL, "read fatal %d:%s", errno, strerror(errno));
            exit(-1);
        }
        close(rwfd_);
    }
    int rwfd_;
    std::string clientip_;
    uint16_t clientport_;
};